﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UMLTouch.Domain.ClassDiagram.Items;
using UMLTouch.Core.UI;

namespace UMLTouch.Domain.ClassDiagram.Controls
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class EntityControl : UserControl, IAnchorable
    {

        EntityItem _item;
        List<ItemAnchor> _anchors;
        List<IAnchorable> _anchoredItems;

        public ItemAnchor Bottom { get; set; }
        public ItemAnchor Top { get; set; }
        public ItemAnchor Right { get; set; }
        public ItemAnchor Left { get; set; }

        public IEnumerable<ItemAnchor> Anchors
        {
            get
            {
                return _anchors;
            }
        }

        public IEnumerable<IAnchorable> AnchoredItems
        {
            get
            {
                return _anchoredItems;
            }

        }

        protected EntityControl()
        {
            InitializeComponent();
            _anchoredItems = new List<IAnchorable>();
            _anchors = new List<ItemAnchor>();
          
        }

        public EntityControl(EntityItem item)
            : this()
        {
            _item = item;

            double a = Width / 2;
            double b = Height / 2;

            Point top = new Point(_item.Position.X + a, _item.Position.Y);
            Point bottom = new Point(_item.Position.X + a, _item.Position.Y + Height);
            Point left = new Point(_item.Position.X, _item.Position.Y + b);
            Point right = new Point(_item.Position.X + Width, _item.Position.Y + b);

            Bottom = new ItemAnchor(this, ItemAnchorDirection.Bottom, bottom);
            Top = new ItemAnchor(this, ItemAnchorDirection.Top, top);
            Left = new ItemAnchor(this, ItemAnchorDirection.Left, left);
            Right = new ItemAnchor(this, ItemAnchorDirection.Right, right);

           

            _anchors.Add(Bottom);
            _anchors.Add(Top);
            _anchors.Add(Left);
            _anchors.Add(Right);
           
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (_item != null)
            {
                SetValue(InkCanvas.LeftProperty, _item.Position.X);
                SetValue(InkCanvas.TopProperty, _item.Position.Y); 
            }
            SetValue(Panel.ZIndexProperty, 1);

            //anchor1.SetValue(Canvas.LeftProperty, Top.Position.X - _item.Position.X);
            //anchor1.SetValue(Canvas.TopProperty, Top.Position.Y - _item.Position.Y);

            //anchor2.SetValue(Canvas.LeftProperty, Bottom.Position.X - _item.Position.X);
            //anchor2.SetValue(Canvas.TopProperty, Bottom.Position.Y - _item.Position.Y);

            //anchor3.SetValue(Canvas.LeftProperty, Left.Position.X - _item.Position.X);
            //anchor3.SetValue(Canvas.TopProperty, Left.Position.Y - _item.Position.Y);

            //anchor4.SetValue(Canvas.LeftProperty, Right.Position.X - _item.Position.X);
            //anchor4.SetValue(Canvas.TopProperty, Right.Position.Y - _item.Position.Y);

        }






        #region IAnchorable Members


        public Boolean ConnectItem(IAnchorable item)
        {
            #region Old
            var anchorsPairs = (from thisAnc in _anchors
                                from destAnc in item.Anchors
                                let dist = Math.Abs((thisAnc.Position - destAnc.Position).Length)
                                where dist<=60 &&  destAnc.Direction == ItemAnchorDirection.Single
                                orderby dist ascending
                                select new { dist, thisAnc, destAnc }).ToList();


           
            var anchorsPair = anchorsPairs.FirstOrDefault();

            if (anchorsPair != null)
            {
                anchorsPair.destAnc.ConnectedAnchors.Add(anchorsPair.thisAnc);
                anchorsPair.thisAnc.ConnectedAnchors.Add(anchorsPair.destAnc);
                anchorsPair.destAnc.Position = anchorsPair.thisAnc.Position;
            }

            return (anchorsPair != null); 
            #endregion
            return false;
        }



        #endregion

        private void UserControl_Drop(object sender, DragEventArgs e)
        {

        }

        #region IAnchorable Members


        public void Move(Vector move)
        {
            _item.Position += move;
            foreach (var item in _anchors)
            {
                item.Position += move;
                foreach (var jhkj in item.ConnectedAnchors)
                {
                    jhkj.Position += move;
                }
            }
        }

        #endregion
    }
}
